//
//  GdataParser.m
//  BMW
//
//  Created by up72-2 on 15/10/28.
//  Copyright © 2015年 up72-2. All rights reserved.
//

#import "GdataParser.h"
#import "GDataXMLNode.h"
@implementation GdataParser

- (void)start:(callBackData)data
{
    
    self.arr = [NSMutableArray array];
    
    //获取工程目录的xml文件
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Area"ofType:@"xml"];
    NSData *xmlData = [[NSData alloc] initWithContentsOfFile:filePath];
    
    //使用NSData对象初始化
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:nil];
    
    //获取根节点（root）
    GDataXMLElement *rootElement = [doc rootElement];
    
    
    NSArray *areas = [rootElement elementsForName:@"Areas"];
    
    for (GDataXMLElement *area in areas) {
        for (GDataXMLElement *pro in [area elementsForName:@"Province"]) {
            NSMutableDictionary *dict = [NSMutableDictionary dictionary];
            //User节点的id属性
            NSString *proId = [[[pro elementsForName:@"ProvinceID"] objectAtIndex:0] stringValue];
            [dict setObject:proId forKey:@"proID"];
            
            //获取name节点的值
            GDataXMLElement *nameElement = [[pro elementsForName:@"ProvinceName"] objectAtIndex:0];
            NSString *name = [nameElement stringValue];
            [dict setObject:name forKey:@"proName"];
            
            NSMutableArray *arry = [NSMutableArray array];
            for (GDataXMLElement *citys in [pro elementsForName:@"Citys"]) {
                for (GDataXMLElement *city in [citys elementsForName:@"City"]) {
                    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
                    
                    
                    NSString *proId = [[[city elementsForName:@"CityID"] objectAtIndex:0] stringValue];
                    [dic setObject:proId forKey:@"cityID"];
                    
                    //获取name节点的值
                    GDataXMLElement *nameElement = [[city elementsForName:@"CityName"] objectAtIndex:0];
                    NSString *name = [nameElement stringValue];
                    [dic setObject:name forKey:@"cityName"];
                    
                    [arry addObject:dic];
                }
            }
            [dict setObject:arry forKey:@"city"];
            
            
            [self.arr addObject:dict];
        }
    }
    
    data(self.arr);
}
@end
